﻿// <copyright file="TISourceFilesParser.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Provides a source files parser for TI project files.
// </summary>

namespace DoxyOutParser.CodeCheck
{
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.IO;
	using System.Linq;
	using System.Text;

	/// <summary>
	/// Get sources from a TI project file.
	/// </summary>
	public class TISourceFilesParser : ISourceFilesParser
	{
		/// <summary>
		/// Path to the TI project file.
		/// </summary>
		private string tiProjectFile;

		/// <summary>
		/// Initializes a new instance of the TISourceFilesParser class.
		/// </summary>
		/// <param name="projectFile">Path to the TI project file which needs parsing.</param>
		public TISourceFilesParser(string projectFile)
		{
			// TODO Check the value of string and eventually check if exist or not.
			this.tiProjectFile = projectFile;
		}

		#region ISourceFilesParser Members

		/// <summary>
		/// Gets the list of source files specified in the project file.
		/// </summary>
		/// <value>No set available.</value>
		/// <returns>The generated list.</returns>
		public List<string> SourceFilesList
		{
			get
			{
				List<string> sourceFilesList = new List<string>();

				// Using of StreamReader to parse the file
				StreamReader tiProjectStreamReader = new StreamReader(this.tiProjectFile);
				string line = tiProjectStreamReader.ReadLine();

				while (line != null)
				{
					if (line.StartsWith("Source=\"", StringComparison.Ordinal))
					{
						int startIndex = line.IndexOf("Source=\"", StringComparison.Ordinal);
						int endIndex = line.IndexOf("\"", "Source=\"".Length, StringComparison.Ordinal);
						if (startIndex != -1 && endIndex != -1)
						{
							// Check if source is a ".c" file
							if (line.Substring(endIndex - 2, 2).Equals(".c"))
							{
								sourceFilesList.Add(line.Substring(startIndex + "Source=\"".Length, (endIndex - startIndex - "Source=\"".Length)));
							}
						}
					}

					line = tiProjectStreamReader.ReadLine();
				}

				return sourceFilesList;
			}
		}

		#endregion
	}
}
